/**
 *
 */
package com.teamall.web.xxgl.controller;

import com.teamall.framework.util.UploadFileUtil;
import com.teamall.framework.util.Utils;
import com.teamall.web.main.vo.LoginUserInfo;
import com.teamall.web.sys.db.po.SysFilePO;
import com.teamall.web.sys.db.po.SysInfoPO;
import com.teamall.web.sys.db.po.SysPhotoPO;
import com.teamall.web.sys.service.ISysFileService;
import com.teamall.web.sys.service.ISysPhotoService;
import com.teamall.web.xxgl.db.po.AidReportChannelPO;
import com.teamall.web.xxgl.db.po.AidReportChannelRelPO;
import com.teamall.web.xxgl.db.po.AidReportDataPO;
import com.teamall.web.xxgl.service.IAidReportChannelRelService;
import com.teamall.web.xxgl.service.IAidReportChannelService;
import com.teamall.web.xxgl.service.IAidReportDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.teamall.core.common.JModelAndView;
import com.teamall.core.common.Page;
import com.teamall.framework.exception.BaseAppException;
import com.teamall.web.xxgl.db.po.CodeAidReportDataPO;
import com.teamall.web.xxgl.service.ICodeAidReportDataService;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <Description>信息报送 <br>
 *
 * @author pan.xiaobo<br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate 2017年8月18日 <br>
 * @see com.teamall.web.xxgl.controller <br>
 * @since V1.0<br>
 */
@Controller
@RequestMapping("/xxbs")
public class XxbsController {

    @Autowired
    private ICodeAidReportDataService codeAidReportDataService;
    @Autowired
    private IAidReportDataService aidReportDataService;
    @Autowired
    private ISysFileService sysFileService;
    @Autowired
    private IAidReportChannelService reportChannelService;
    @Autowired
    private IAidReportChannelRelService aidReportChannelRelService;

    @RequestMapping("/index")
    public ModelAndView index() throws BaseAppException {
        ModelAndView modelAndView = new JModelAndView("/xxgl/xxbs_index");
        modelAndView.addObject("reportChannelList", reportChannelService.selectByArg(new AidReportChannelPO()));
        return modelAndView;
    }

    @RequestMapping(value = "/add", consumes = "multipart/form-data")
    @ResponseBody
    public int getlistByType(HttpServletRequest request, AidReportDataPO record) throws BaseAppException {
        LoginUserInfo loginUserInfo = (LoginUserInfo) request.getSession().getAttribute("loginUserInfo");
        record.setReportStaff(loginUserInfo.getUserObjId());
        List<Map> resultList = UploadFileUtil.saveFiles1(request);
        for (Map<String, String> map : resultList) {
            SysFilePO po = new SysFilePO();
            po.setPhotoCode(map.get("imgId"));
            po.setFilePath(map.get("realPath"));
            po.setFileName(map.get("fileName"));
            sysFileService.add(po);
            record.setAttachment(map.get("imgUrl"));
        }
        if (Utils.isEmpty(record.getReportId())) {
            record.setReportDate(new Date());
            return aidReportDataService.add(record);
        } else {
            return aidReportDataService.update(record);
        }
    }

    @RequestMapping("/batchDel")
    @ResponseBody
    public int batchDel(String ids) throws BaseAppException {
        return aidReportDataService.batchDel(ids);
    }

    @RequestMapping("/infoAdd")
    public ModelAndView infoAdd(Integer id) throws BaseAppException {
        ModelAndView modelAndView = new JModelAndView("/xxgl/xxbs_info_add");
        if (Utils.notEmpty(id)) {
            AidReportDataPO aidReportDataPO = aidReportDataService.selectByPrimaryKey(id);
            modelAndView.addObject("aidReportDataPO", aidReportDataPO);
        }
        return modelAndView;
    }

    @ResponseBody
    @RequestMapping("/qryReportData")
    public Page<CodeAidReportDataPO> qryReportData(
            CodeAidReportDataPO qryRecord, Page<CodeAidReportDataPO> resultPage)
            throws BaseAppException {
        // 查詢条件：村id,贫困户名等

        resultPage = codeAidReportDataService.selectByArgAndPage(qryRecord, resultPage);
        for(CodeAidReportDataPO codeAidReportDataPO:resultPage.getResultList()){
            // 报送数量
            AidReportChannelRelPO aidReportChannelRelPO = new AidReportChannelRelPO();
            aidReportChannelRelPO.setReportId(codeAidReportDataPO.getReportId());
            int num = aidReportChannelRelService.selectByArg(aidReportChannelRelPO).size();
            codeAidReportDataPO.setReportNum(num);
            // 图片真实名字
            String attachment = codeAidReportDataPO.getAttachment();
            if(Utils.notEmpty(attachment)){
                String fileId = attachment.split("=")[1];
                SysFilePO po = new SysFilePO();
                po.setPhotoCode(fileId);
                SysFilePO resultPO = sysFileService.selectByArg(po).get(0);
                codeAidReportDataPO.setFileName(resultPO.getFileName());
            }
        }
        return resultPage;
    }

    @ResponseBody
    @RequestMapping("/getReports")
    public List<AidReportChannelRelPO> getReports(Integer id) throws BaseAppException {
        AidReportChannelRelPO aidReportChannelRelPO = new AidReportChannelRelPO();
        aidReportChannelRelPO.setReportId(id);
        return aidReportChannelRelService.selectByArg(aidReportChannelRelPO);
    }

    @ResponseBody
    @RequestMapping("/setReports")
    public boolean setReports(Integer reportId, String ids) throws BaseAppException {
        if (Utils.isEmpty(reportId)) {
            return false;
        }
        String[] idStrs = ids.split(",");
        for (String chanelId : idStrs) {
            AidReportChannelRelPO newAiPo = new AidReportChannelRelPO();
            newAiPo.setReportId(reportId);
            newAiPo.setReportId(reportId);
            newAiPo.setChannelId(Integer.valueOf(chanelId));
            newAiPo.setRefDate(new Date());
            aidReportChannelRelService.add(newAiPo);
        }
        return true;
    }
}
